﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Xml;
using System.Xml.Xsl;

public partial class KMrpt25 : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.OperationArea.GetStore().DataSource = db.DM_GetDateSet("select OperationAreaID,OperationAreaName from Bas_OperationArea");
        this.arealist.GetStore().DataSource = db.DM_GetDateSet("select * from Bas_Area where Level = 1 order by areaname");
        this.txtstock.GetStore().DataSource = db.DM_GetDateSet("select stockid,stockname from bas_stock");
        this.txtoutstatus.GetStore().DataSource = db.DM_GetDateSet("select stateid,cnstatename as statename from Sys_State where statefixflag='auditresult'");
        this.txtinstatus.GetStore().DataSource = db.DM_GetDateSet("select stateid,cnstatename as statename from Sys_State where statefixflag='auditresult'");
        this.txtmatkind.GetStore().DataSource = db.DM_GetDateSet("select distinct kindname from km_v_mat");
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
            this.txtbdate.Text = DateTime.Now.ToString();

            this.txtedate.Text = DateTime.Now.ToString();
            //this.txtbjhdate.Text = DateTime.Now.ToString("yyyy-MM-01");
            //this.txtejhdate.Text = DateTime.Now.ToString();
        }
    }

    [DirectMethod(Namespace = "CompanyX")]
    public void binStock(string area)
    {
        string sql = "";
        if (area == "[]")
        {
            sql = @"select stockid,stockname from bas_stock";
        }
        else
        {
            area = area.Replace("[", "");
            area = area.Replace("]", "");
            area = area.Replace("\"", "'");

            sql = @"select stockid,stockname from bas_stock where operationareaid in (" + area + ") order by stockname";
        }
        //ShowBox(sql);
        this.txtstock.GetStore().DataSource = db.DM_GetDateSet(sql);
        this.st_stock.DataBind();
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string bdate, edate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            bdate = "";
        else
            bdate = txtbdate.Text;

        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            edate = "";
        else
            edate = txtedate.Text;

        //if (Convert.ToDateTime(txtbjhdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        //    sjhdaate = "";
        //else
        //    sjhdaate = txtbjhdate.Text;

        //if (Convert.ToDateTime(txtejhdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        //    ejhdaate = "";
        //else
        //    ejhdaate = txtejhdate.Text;
        string bigarea = StrSplitParam(OperationArea);
        string areas = StrSplitParam(arealist);
        string shop = StrSplitParam(txtstock);
        string outstatus = StrSplitParam(this.txtoutstatus);
        string instatus = StrSplitParam(this.txtinstatus);
        string matkind = StrSplitParam(this.txtmatkind);
        DataSet ds = db.DM_GetDateSet("KM_Inv_StockMove '" + bdate + "','" + edate + "','" + bigarea + "','"
            + areas + "','" + shop + "','" + instatus + "','" + outstatus + "','" + matkind + "',1");
        this.Store1.DataSource = ds;
        this.Store1.DataBind();
        //DynaGridResult(ds);
    }

    #region 动态加载报表显示结果
    private void DynaGridResult(DataSet ds)
    {
        if (ds == null) return;

        Store store = this.Store1;
        GridPanel grid = this.GridPanel1;
        store.Reader.Clear();
        grid.ColumnModel.Columns.Clear();
        store.Model.Clear();
       
        Model md = new Model();
        List<ColumnBase> list = new List<ColumnBase>();

        DataTable dt = ds.Tables[0];
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            //根据结果集创建Store
            ModelField field = new ModelField();
            field.Name = dt.Columns[i].ToString();
            md.Fields.Add(field);
            //创建grid colums
            Column cl = new Column();
            cl.Text = dt.Columns[i].ToString();
            cl.DataIndex = dt.Columns[i].ToString();
            list.Add(cl);
        }
        store.Model.Add(md);
        store.DataSource = ds;
        store.DataBind();
        grid.ColumnModel.Columns.Add(list);
        grid.Render();
    }
    #endregion
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    protected void ToExcel(object sender, EventArgs e)
    {
        InsActLog(mid, "导出");
        string json = this.Hidden1.Value.ToString();
        StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
        XmlNode xml = eSubmit.Xml;

        this.Response.Clear();
        this.Response.ContentType = "application/vnd.ms-excel";
        this.Response.AddHeader("Content-Disposition", "attachment; filename=" + this.Title + ".xls");

        XslCompiledTransform xtExcel = new XslCompiledTransform();

        xtExcel.Load(Server.MapPath("Excel.xsl"));
        xtExcel.Transform(xml, null, this.Response.OutputStream);
        this.Response.End();
    }
}